using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using Voodoo.Sauce.Internal;

namespace Voodoo.Analytics
{
	internal static class AnalyticsTextFileLogger
	{
		private static string _apiRequestResponseLoggerPath = "";

		private static bool _isDebugMode = false;

		private const string TAG = "Analytics - TextLogger";

		private const string BEGIN_API = "-----BEGIN API LOG-----";

		private const string END_API = "-----END API LOG------";

		private const string BEGIN_REQUEST_HEADER = "-----BEGIN REQUEST HEADER-----";

		private const string END_REQUEST_HEADER = "-----END REQUEST HEADER-----";

		private const string BEGIN_REQUEST_BODY = "-----BEGIN REQUEST BODY-----";

		private const string END_REQUEST_BODY = "-----END REQUEST BODY-----";

		private const string BEGIN_RESPONSE_BODY = "-----BEGIN RESPONSE BODY-----";

		private const string END_RESPONSE_BODY = "-----END RESPONSE BODY-----";

		private const string BEGIN_RESPONSE_STATUS = "-----BEGIN RESPONSE STATUS-----";

		private const string END_RESPONSE_STATUS = "-----END RESPONSE STATUS-----";

		private const string BEGIN_EXCEPTION = "-----BEGIN API EXCEPTION-----";

		private const string END_EXCEPTION = "-----END API EXCEPTION-----";

		private const string FILE_NAME = "analytics_export_logs.txt";

		internal static void ConfigureAutomatedTextLogger(string path, bool isDebugMode)
		{
			_apiRequestResponseLoggerPath = path;
			_isDebugMode = isDebugMode;
		}

		private static void WriteRequestHeader(List<string> stringToWrite, string requestHeaderString)
		{
			stringToWrite.Add("-----BEGIN REQUEST HEADER-----");
			stringToWrite.Add(requestHeaderString);
			stringToWrite.Add("-----END REQUEST HEADER-----");
		}

		private static void WriteRequestBody(List<string> stringToWrite, string requestBodyString)
		{
			stringToWrite.Add("-----BEGIN REQUEST BODY-----");
			stringToWrite.Add(requestBodyString);
			stringToWrite.Add("-----END REQUEST BODY-----");
		}

		private static void WriteResponseStatusCode(List<string> stringToWrite, HttpStatusCode statusCode)
		{
			stringToWrite.Add("-----BEGIN RESPONSE STATUS-----");
			stringToWrite.Add($"{(int)statusCode} - {statusCode.ToString()}");
			stringToWrite.Add("-----END RESPONSE STATUS-----");
		}

		private static void WriteResponseBody(List<string> stringToWrite, string responseBody)
		{
			stringToWrite.Add("-----BEGIN RESPONSE BODY-----");
			stringToWrite.Add(responseBody);
			stringToWrite.Add("-----END RESPONSE BODY-----");
		}

		public static async void LogToText(HttpResponseMessage responseMessage)
		{
			if (_isDebugMode)
			{
				VoodooLog.LogWarning(Module.ANALYTICS, "Analytics - TextLogger", "LogToText called with response " + responseMessage);
			}
			List<string> stringsToWrite = new List<string>();
			try
			{
				stringsToWrite.Add("-----BEGIN API LOG-----");
				WriteRequestHeader(stringsToWrite, responseMessage.RequestMessage.Content.Headers.ToString());
				List<string> stringToWrite = stringsToWrite;
				WriteRequestBody(stringToWrite, await responseMessage.RequestMessage.Content.ReadAsStringAsync());
				WriteResponseStatusCode(stringsToWrite, responseMessage.StatusCode);
				stringToWrite = stringsToWrite;
				WriteResponseBody(stringToWrite, await responseMessage.Content.ReadAsStringAsync());
				stringsToWrite.Add("-----END API LOG------");
				WriteAnalyticsLogsToFile(stringsToWrite);
			}
			catch (Exception)
			{
			}
		}

		public static void LogExceptionToText(Exception exception)
		{
			if (_isDebugMode)
			{
				VoodooLog.LogWarning(Module.ANALYTICS, "Analytics - TextLogger", "LogExceptionToText called with exception: " + exception);
			}
			WriteAnalyticsLogsToFile(new List<string>
			{
				"-----BEGIN API EXCEPTION-----",
				exception.ToString(),
				"-----END API EXCEPTION-----"
			});
		}

		private static void WriteAnalyticsLogsToFile(List<string> stringsToWrite)
		{
		}
	}
}
